package es.yporqueno.cargadores {
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import flash.utils.getQualifiedClassName;

	public class AbstractCargadorObjetos extends AbstractCargador {
		
		protected var _loader:Loader;
		protected var _posH:String
		protected var _posV:String;
		
		public function AbstractCargadorObjetos() {
			// Clase Abstracta. No puede ser instanciada
			if (getQualifiedClassName(this) == "com.uveproducers.cargadores::AbstractCargadorObjetos") {
				throw new Error("AbstractCargadorObjetos es una clase abstracta y no puede ser instanciada");
				return;
			}
		}

		// Funciones Publicas -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
		public function limpiar():void{
			if(_loading){
				stop();
			}
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loaderComplete);
			_loader.contentLoaderInfo.removeEventListener(HTTPStatusEvent.HTTP_STATUS, loaderStatus);
			_loader.contentLoaderInfo.removeEventListener(Event.INIT, loaderInit);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, loaderError);
			_loader.contentLoaderInfo.removeEventListener(Event.OPEN, loaderOpen);
			_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, loaderProgress);
			_loader.contentLoaderInfo.removeEventListener(Event.UNLOAD, loaderUnload);
			_loader = null;
		}
		
		public function stop():void{
			_loader.close();
			_loading = false;
		}
		
		
		// Funciones Privadas -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
		// 
		protected function init():void{
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
			_loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, loaderStatus);
			_loader.contentLoaderInfo.addEventListener(Event.INIT, loaderInit);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loaderError);
			_loader.contentLoaderInfo.addEventListener(Event.OPEN, loaderOpen);
			_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loaderProgress);
			_loader.contentLoaderInfo.addEventListener(Event.UNLOAD, loaderUnload);
		}

		override protected function loaderComplete(e:Event):void {
			// Coloco la imagen en la posicion indicada, en horizontal y vertical
			if (_posH == "LEFT") {
				//
			} else if (_posH == "CENTER") {
				e.target.content.x -= e.target.width / 2;
			} else if (_posH == "RIGHT") {
				e.target.content.x -= e.target.width;
			}

			if (_posV == "TOP") {
				//
			} else if (_posV == "CENTER") {
				e.target.content.y -= e.target.height / 2;
			} else if (_posV == "BOTTOM") {
				e.target.content.y -= e.target.height;
			}
			super.loaderComplete(e);
		}
		
		
		
		
	}
}